//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by Snow on 16/7/18.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //手势识别器 UIGestureRecognizer 是所有手势的基类
    /* 
     1.轻拍
     2.长按
     3.轻扫
     4.平移
     5.捏合
     6.旋转
     7.屏幕边缘
     */
    
    /* 轻拍 */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//创建手势
    tap.numberOfTapsRequired = 2;//点击几次触发(默认为1)
    tap.numberOfTouchesRequired = 2;//几个手指触发(按option - alt)默认为1
    [self.view addGestureRecognizer:tap];//1
    
    /* 长按 */
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];//创建手势
    [self.view addGestureRecognizer:longPress];
    longPress.minimumPressDuration = 3;//最少按几秒响应 默认是5秒
    longPress.allowableMovement = 10;//默认是10像素 长按失败前 最大的移动距离
    
    /* 轻扫 */
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];//默认向右
    [self.view addGestureRecognizer:swipe];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;//设置向左轻扫 可以设置多个 但前提是多个方向所触发的行为要相同 如果多个方法的行为不同 创建多个轻扫的手势
    //左右与上下有冲突
    //练习:ImageView添加轻扫换图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 355, 647)];
    imageView.image = [UIImage imageNamed:@"21263729ea.jpg"];
    [self.view addSubview:imageView];
    UISwipeGestureRecognizer *swipeChangeImageRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImageLeft:)];
//    [imageView addGestureRecognizer:swipeChangeImageRight];
    imageView.userInteractionEnabled = YES;
    //按位枚举, 4个选项-> 1 1 1 1
    //全选的情况 1(3) 1(2) 1(1) 1(0)
    UISwipeGestureRecognizer *swipeChangeImageLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeImageLeft:)];
    swipeChangeImageLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//    [imageView addGestureRecognizer:swipeChangeImageLeft];
    
    /* 平移 */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    [imageView addGestureRecognizer:pan];
    
    /* 捏合 */
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
//    [imageView addGestureRecognizer:pinch];
    /* 旋转 */
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//    [imageView addGestureRecognizer:rotation];
    /* 屏幕边缘手势 */
    UIScreenEdgePanGestureRecognizer *egde = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction:)];
    egde.edges = UIRectEdgeAll;
    [self.view addGestureRecognizer:egde];
    
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫");
}
//- (void)swipeChangeImageRight:(UISwipeGestureRecognizer *)swipe
//{
//    UIImageView *imageView = (UIImageView *)swipe.view;
//    imageView.image = [UIImage imageNamed:@"1-13022G43250-50.jpg"];
//}
- (void)swipeChangeImageLeft:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        UIImageView *imageView = (UIImageView *)swipe.view;
        imageView.image = [UIImage imageNamed:@"1-13022G43250-50.jpg"];
    } else {
        UIImageView *imageView = (UIImageView *)swipe.view;
        imageView.image = [UIImage imageNamed:@"21263729ea.jpg"];
    }
}
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateBegan) {
        //平移开始
    }
    if (pan.state == UIGestureRecognizerStateChanged) {
        //平移手势 根据其他View上移动的过程 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        //根据point使Pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        [pan setTranslation:point inView:pan.view];
        NSLog(@"%@", NSStringFromCGPoint(point));
        
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        //平移结束
        pan.view.transform = CGAffineTransformIdentity;
    }
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合");
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //设置缩放倍数
    pinch.scale = 1;
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    //旋转中 根据赏析玄幻继续改变
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 0;//旋转的弧度置为0
}
- (void)edgeAction:(UIScreenEdgePanGestureRecognizer *)edge
{
    NSLog(@"屏幕边缘手势");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
